<?php

class bookmarks_BookmarkController extends Zend_Controller_Action
{

	function init() {
	header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
	header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); // Date in the past
	$this->getHelper('viewRenderer')->setNoRender();
	}

	function createAction() {

		require_once('bookmarks/objects/bookmark.php');
		try{
			$id= Bookmarks_Object_Bookmark::create($this->_getParam('URL'),$this->_getParam('tags'),$this->_getParam('title'),$this->_getParam('description'),$this->_getParam('isPrivate'));
		} catch(Bookmarks_Object_Exception $e) {
			  $data['status'] = 	'KO';
			  $data['error']['num'] = 	$e->getCode();
			  $data['error']['message'] = 	$e->getMessage();
			  echo Zend_Registry::get('dt_serializer')->serialize($data);	
			  return;
		}
		$data['status'] = 	'OK';

	
		//We recreate the status
		$bookmark = new Bookmarks_Object_Bookmark($id);		
		//We publish the bookmark in the user bookmarks list
		Zend_Registry::get('dt_log')->log('Publishing bookmark to the user , id:'.$id,Zend_Log::INFO);
    	require_once('bookmarks/objects/list.php'); 
		Bookmarks_Object_List::publishBookmarkToUser($bookmark);
	
		//If the use is asynchronous, we put a message in a queue
		$config = Zend_Registry::get('dt_config');
		if($config->asynchronous->use) {
			Zend_Registry::get('dt_log')->log('Pushing bookmark in asynchronous mode, id:'.$id,Zend_Log::INFO);
			$queue = Zend_Registry::get('dt_asynchronousQueue');		
			$queue->send(new Myzen_Queue_AsynchronousJobMessage('bookmarks','bookmark','indexing',array('bookmarkId'=>$id)));
		} else { //Else we add the indexing in the stack
			Zend_Registry::get('dt_log')->log('Pushing bookmark in synchronous mode, id:'.$id,Zend_Log::INFO);		
			$this->_helper->actionStack('indexing', 'bookmark','bookmarks', array('bookmarkId'=>$id));
		}
			
		//we can t use status as it is alredy used to say evrything went fine
		$data['bookmark'] =  $bookmark ->asArray();	
	
		echo Zend_Registry::get('dt_serializer')->serialize($data);
		return;
		
	} //end createaction	
 
 
 
 
 
 
 
   //read a particular bookmark (only works when the bookmark is public)
  function readAction() {
    require_once('bookmarks/objects/bookmark.php');
    try{
	  $bookmark = new Bookmarks_Object_Bookmark($this->_getParam('id'));
    } catch(Status_Object_Exception $e) {
      $data['bookmark'] = 	'KO';
      $data['error']['num'] = 	$e->getCode();
      $data['error']['message'] = 	$e->getMessage();
      echo Zend_Registry::get('dt_serializer')->serialize($data);	
      return;
    }
	
	//we check if the bookmark is visible by the user
	if(!$bookmark->isVisible()) {
      $data['bookmark'] = 	'KO';
      $data['error']['num'] = 	8;
      $data['error']['message'] = 	'Invalid identifier';
      echo Zend_Registry::get('dt_serializer')->serialize($data);	
      return;		
	}
	
	$data['status'] = 	'OK';
	//we can t use bookmark as it is alredy used to say evrything went fine
	$data['bookmark'] =  $bookmark->asArray();	
	
	echo Zend_Registry::get('dt_serializer')->serialize($data);
    return;
  } //end read action  
 
 
 
 
 
 
  

  function deleteAction() {
    require_once('bookmarks/objects/bookmark.php');
    try{
	  $bookmark = new Bookmarks_Object_Bookmark($this->_getParam('id'));
	  Zend_Registry::get('dt_log')->log('deleting bookmark '.$this->_getParam('id'),Zend_Log::INFO);
	  //we start by deleting the status object
	  $n = $bookmark->delete();
	  if(1 != $n) Zend_Registry::get('dt_log')->log('Important issue while deleting bookmark '.$status->getId().'. '.$n.' bookmarks deleted', Zend_Log::CRIT);
	  
	  	//then we delete the status from the timelines
		require_once('bookmarks/objects/List.php');
		$n = Bookmarks_Object_List::deleteBookmark($bookmark);	
		Zend_Registry::get('dt_log')->log($n.' occurences of the status deleted from the stream database',Zend_Log::INFO);
	  
	  
    } catch(Bookmark_Object_Exception $e) {
      $data['status'] = 	'KO';
      $data['error']['num'] = 	$e->getCode();
      $data['error']['message'] = 	$e->getMessage();
      echo Zend_Registry::get('dt_serializer')->serialize($data);	
      return;
    }

    //No issue, we just return ok
    $data['status'] = 	'OK';    
    echo Zend_Registry::get('dt_serializer')->serialize($data);	
    return;    
    
  }


	function updateAction(){
		Zend_Registry::get('dt_log')->log('updating bookmark:'.$this->_getParam('bookmarkId'),Zend_Log::NOTICE);
		require_once('bookmarks/objects/bookmark.php');
		$bookmark = new Bookmarks_Object_Bookmark($this->_getParam('bookmarkId'));		
		$bookmark->update($this->_getAllParams());
		//we recreate the bookmark object
		$newBookmark = new Bookmarks_Object_Bookmark($this->_getParam('bookmarkId'));
		
		//we delete the tag indexing and recreate it 
		//we don t delete the user indexing to avoid having the bookmark pushed back to the top of the list
		require_once('bookmarks/objects/list.php');
		Bookmarks_Object_List::deleteBookmarkTags($bookmark);	

		
		//If the use is asynchronous, we put a message in a queue
		$config = Zend_Registry::get('dt_config');
		if($config->asynchronous->use) {
			Zend_Registry::get('dt_log')->log('Pushing bookmark in asynchronous mode, id:'.$id,Zend_Log::INFO);
			$queue = Zend_Registry::get('dt_asynchronousQueue');		
			$queue->send(new Myzen_Queue_AsynchronousJobMessage('bookmarks','bookmark','indexing',array('bookmarkId'=>$bookmark->getId())));
		} else { //Else we add the indexing in the stack
			Zend_Registry::get('dt_log')->log('Pushing bookmark in synchronous mode, id:'.$id,Zend_Log::INFO);		
			$this->_helper->actionStack('indexing', 'bookmark','bookmarks', array('bookmarkId'=>$bookmark->getId()));
		}
		
		
		
		//No issue, we return the updated bookmark
		$data['status'] = 	'OK';   
		
		$data['bookmark'] =  $newBookmark->asArray();
		echo Zend_Registry::get('dt_serializer')->serialize($data);	
		return;   		
		
	}



  function indexingAction() {
	
	Zend_Registry::get('dt_log')->log('indexing bookmark:'.$this->_getParam('bookmarkId'),Zend_Log::INFO);
	require_once('bookmarks/objects/bookmark.php');
    require_once('bookmarks/objects/list.php');
	$bookmark = new Bookmarks_Object_Bookmark($this->_getParam('bookmarkId'));

	Bookmarks_Object_List::publishBookmarkToTags($bookmark);		
	
  }
  
	//used to create a new bookmark from a status that contained a specific keyword (usually link or bookmark)

	function createfromstatusAction() {
		$aStatus = $this->_getParam('status');
		Zend_Registry::get('dt_log')->log('creating new bookmark from status: '.serialize($aStatus),Zend_Log::INFO);	
		require_once('bookmarks/objects/bookmark.php');
		$idList = Bookmarks_Object_Bookmark::createFromStatus($aStatus);
		
		Zend_Registry::get('dt_log')->log('List of ids returned: '.serialize($idList),Zend_Log::INFO);		
		
		if(!empty($idList)) {
		//If the use is asynchronous, we put a message in a queue
		$useAsynch = Zend_Registry::get('dt_config')->asynchronous->use;
		if($useAsynch) $queue = Zend_Registry::get('dt_asynchronousQueue');
			foreach($idList as $num => $id) {
				if(empty($id)) continue;
				
				//Bookmark publishing to the user can t be done asynchrnously (as he expect to see it asap)
				//We recreate the status
				$bookmark = new Bookmarks_Object_Bookmark($id);		
				//We publish the bookmark in the user bookmarks list
				Zend_Registry::get('dt_log')->log('Publishing bookmark to the user , id:'.$id,Zend_Log::INFO);
				require_once('bookmarks/objects/list.php'); 
				Bookmarks_Object_List::publishBookmarkToUser($bookmark);
				
				if($useAsynch) {
					Zend_Registry::get('dt_log')->log('Pushing bookmark in asynchronous mode, id:'.$id,Zend_Log::INFO);			
					$queue->send(new Myzen_Queue_AsynchronousJobMessage('bookmarks','bookmark','indexing',array('bookmarkId'=>$id)));
				} else { //Else we add the indexing in the stack
					Zend_Registry::get('dt_log')->log('Pushing bookmark in synchronous mode, id:'.$id,Zend_Log::INFO);		
					$this->_helper->actionStack('indexing', 'bookmark','bookmarks', array('bookmarkId'=>$id));
				}		
			} //end foreach
		} //end list of identifiers	
	}





  
} //End class
